/**
 * @Author 韦建韩
 * @Date 2022/5/20
 */
package com.example.devops.web.controller.applicationAdmin;

import com.example.devops.Manage.HttpPortCache;
import com.example.devops.entity.common.Result;
import com.example.devops.entity.pojo.DiskData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("resource")
public class ResourceController {
    @Autowired
    HttpPortCache httpPortCache;

    @RequestMapping("getUserPort")
    public Result getUserPort(){
        httpPortCache.init();
        Map<String, Boolean> portMap = httpPortCache.getPortMap();
        List<String> list = httpPortCache.getPortList();
        List<Integer> userList = new ArrayList<>();
        for (String s : list) {
            if(Objects.isNull(portMap.get(s)) || !portMap.get(s)){
                userList.add(0);
                continue;
            }
            userList.add(1);
        }
        return Result.doCreate(userList);
    }

    @RequestMapping("diskData")
    public Result getDiskData(){
        List<DiskData> list = new ArrayList<>();
        File root =File.listRoots()[0];
        File[] disks = root.listFiles();
        for(File file : disks)
        {
            DiskData d = new DiskData();
            d.setFile(file.getPath());
            d.setFree(file.getFreeSpace() / 1024 / 1024 + "M");
            d.setUsed(file.getUsableSpace() / 1024 / 1024 + "M");
            d.setTotal(file.getTotalSpace() / 1024 / 1024 + "M");
            list.add(d);
        }
        return Result.doCreate(list);
    }
}
